package com.bingxue.edu.common.wechat;

import com.bingxue.edu.common.config.WechatProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import org.springframework.stereotype.Service;

/**
 * @author panbo
 * @since 2025/2/10
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class WxMessageManager {

    private final WechatProperties wechatProperties;
    private final WxCpService wxCpService;

    public void sendMessage(WxCpMessage message) {
        try {
            wxCpService.getMessageService().send(message);
        } catch (Exception e) {
            log.error("发送企业微信通知失败, {}", e.getMessage(), e);
        }
    }

    public WxCpMessage buildMiniAppMessage(String toUser) {
        WxCpMessage wxCpMessage = new WxCpMessage();
        wxCpMessage.setSafe("0");
        wxCpMessage.setMsgType(WxConsts.KefuMsgType.MINIPROGRAM_NOTICE);
        wxCpMessage.setAppId(wechatProperties.getEmployeeAppId());
        wxCpMessage.setToUser(toUser);
        wxCpMessage.setEmphasisFirstItem(true);

        return wxCpMessage;
    }
}
